package com.ujiuye.utils;

//分页工具类
public class PageTool {
	private int currentPage;//当前页
	private int pageSize;//每页显示的条数  自定义大小
	private int startIndex;//起始索引   startIndex = (currentPage-1)*pageSize
	private int totalCount;//总条数  查询数据库得到select count(*) from 表名;
	private int totalPage;//总页数 计算: (totalCount%pageSize == 0) ? (totalCount/pageSize):(totalCount/pageSize+1)
	private int prePage;//上一页 currentPage-1
	private int nextPage;//下一页 currentPage+1
	
	public PageTool(String currentPage, int totalCount) {
		this.totalCount = totalCount;
		//自定义显示的条数
		this.pageSize = 2;
		initCurrentPage(currentPage);//当前页
		initTotalPage();
		initStartIndex();
		initPrePage();
		initNextPage();
	}

	//给当前页进行初始化
	private void initCurrentPage(String currentPage) {
		//判断
		if(currentPage == null) {
			this.currentPage = 1;
		}else {
			//赋值第几页
			this.currentPage = Integer.valueOf(currentPage);
		}
	}
	
	//计算总页数
	private void initTotalPage() {
		//计算
		this.totalPage = (totalCount%pageSize == 0) ? (totalCount/pageSize):(totalCount/pageSize+1);
	}
	
	//上一页
	private void initPrePage() {
		//判断
		if(currentPage == 1) {
			//赋值第一页
			this.prePage = 1;
		}else {
			this.prePage = currentPage - 1;//2
		}
	}
	
	//下一页
	private void initNextPage() {
		//判断
		if(currentPage == totalPage) {
			//赋值第一页
			this.nextPage = totalPage;
		}else {
			this.nextPage = currentPage + 1;
		}
	}

	//起始索引
	private void initStartIndex() {
		//计算
		this.startIndex = (currentPage-1) * pageSize;
	}
	
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getStartIndex() {
		return startIndex;
	}
	public void setStartIndex(int startIndex) {
		this.startIndex = startIndex;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public int getPrePage() {
		return prePage;
	}
	public void setPrePage(int prePage) {
		this.prePage = prePage;
	}
	public int getNextPage() {
		return nextPage;
	}
	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}
}
